// pages/driver/wallet/wallet.ts
import { getBalance, getMoneyDetails, topUp, withdraw } from '../../../http/api/driverService'
import { throttle } from '../../../utils/util'

const freezeMap: any = {
    0: '否',
    1: '是',
    2: '金额已作废'
}

Page({
    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        money: 0,
        currentMoney: 0,
        page: 1,
        size: 3,
        total: 0
    } as any,

    handlerChange(value: any) {
        this.setData({
            currentMoney: value.detail.value
        })
    },

    // 提现
    toWithdraw: throttle(async function (this: any) {
        const { currentMoney } = this.data
        console.log(currentMoney)
        await withdraw(currentMoney)
    }),

    // 充值
    recharge: throttle(async function (this: any) {
        const { currentMoney } = this.data
        console.log(currentMoney)
        if (currentMoney <= 0) {
            wx.showToast({
                title: '金额需大于0元'
            })
        } else {
            const res: any = await topUp(currentMoney)
            console.log(res, '---res')
            if (res.data.data[0].errmsg != undefined && res.data.data[0].errmsg.includes('对不起')) {
                return
            }
            const that = this
            await wx.requestPayment({
                timeStamp: res.data.data[0].timeStamp.toString(),
                nonceStr: res.data.data[0].nonce,
                package: res.data.data[0].package,
                signType: res.data.data[0].signType,
                paySign: res.data.data[0].paySign,
                success() {
                    wx.showToast({
                        title: '支付成功',
                        icon: 'success',
                        duration: 1000
                    })
                    that.getWallet()
                    that.getData(1, 3)
                },
                fail(res) {
                    console.log(res)
                    wx.showToast({
                        title: '支付失败',
                        icon: 'error',
                        duration: 500
                    })
                }
            })
        }
    }),

    async getWallet() {
        const res: any = await getBalance()
        this.setData({
            money: res.data.data[0].user_money
        })
    },

    async getData(page = 1, pageSize = 3) {
        const res: any = await getMoneyDetails(page, pageSize)
        let { list } = this.data

        if (page == 1) {
            list = []
        }
        if (!list[0].errmsg) {
            res.data.data.map((item: any) => {
                list.push({
                    ...item,
                    not_use_code: freezeMap[item.not_use_code]
                })
            })
        }

        console.log(list, '---list')
        this.setData({
            total: res.data.count,
            list: list
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        this.getData()
        this.getWallet()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */ async onPullDownRefresh() {
        let { size } = this.data
        this.setData({
            page: 1,
            size: 3
        })

        await this.getData(1, size)

        //停止下拉刷新
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */ async onReachBottom() {
        let { page, size, total } = this.data
        console.log(page * size)
        if (page * size >= total) {
            wx.showToast({
                title: '到底啦～'
            })
            return
        }
        page = page + 1
        this.setData({
            page
        })

        await this.getData(page, size)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {}
})
